package com.zyz.forum.service.impl;

import com.zyz.forum.common.AppResult;
import com.zyz.forum.common.ResultCode;
import com.zyz.forum.dao.BoardMapper;
import com.zyz.forum.exception.ApplicationException;
import com.zyz.forum.model.Board;
import com.zyz.forum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BoardServiceImp implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectBaseByNum(Integer num) {
        //  参数校验
        if (num == null || num <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE+"num = " + num);
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用dao层查询数据库中的数据并返回结果
        return boardMapper.selectBaseByNum(num);
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 查询当前板块的贴子数
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            log.warn(ResultCode.ERROR_IS_NULL.toString() + " boardId = " + id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        // 更新帖子数量
        Board boardNew = new Board();
        boardNew.setId(id);
        boardNew.setArticleCount(board.getArticleCount() + 1);
        int row = boardMapper.updateByPrimaryKeySelective(boardNew);
        // 判断更新是否完成
        if (row != 1) {
            log.error(ResultCode.ERROR_SERVICES.toString() + "board更新帖子数据库异常，更新失败");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Board selectById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public void subOneArticleById(Long id) {
        // 参数校验
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 查询当前板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            // 板块不存在
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        // 构造修改对象
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        // 判断是否小于0
        if (updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }
        updateBoard.setUpdateTime(new Date());
        // 调用dao层
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        // 修改成功
    }
}
